package com.fs.sale.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.core.page.PageDomain;
import com.fs.sale.mapper.OrderDetailMapper;
import com.fs.sale.mapper.SaleOrderMapper;
import com.fs.sale.pojo.OrderDetail;
import com.fs.sale.pojo.PriceList;
import com.fs.sale.pojo.SaleOrder;
import com.fs.sale.service.SaleOrderService;
import com.fs.sale.vo.OrderSearch;
import com.fs.sale.vo.SaleOrderItem;
import com.fs.sale.vo.SaleOrderVo;
import com.fs.system.mapper.SysUserMapper;
import com.fs.system.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
//0,1,2,
@Service
public class SaleOrderServiceImpl implements SaleOrderService {
    @Autowired
    SaleOrderMapper saleOrderMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public Page<SaleOrderVo> select(PageDomain pageDomain, OrderSearch orderSearch) {
        Page<SaleOrderVo> page=new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        if(pageDomain.getIsAsc().equalsIgnoreCase("asc")){ //升序
            page.addOrder(OrderItem.asc(pageDomain.getOrderByColumn()));
        }else if(pageDomain.getIsAsc().equalsIgnoreCase("desc")){ //降序
            page.addOrder(OrderItem.desc(pageDomain.getOrderByColumn()));
        }

        String post=sysUserMapper.selectPostNameByUserId(SecurityUtils.getUserId());
        if(post.equals("普通员工")){
            int id=SecurityUtils.getUserId().intValue();
            List<SaleOrderVo> saleOrderVoList=modifyStatus(saleOrderMapper.selectById(id,orderSearch,page));
            page.setRecords(saleOrderVoList);
            return page;
        }
        List<SaleOrderVo> saleOrderVoList=modifyStatus(saleOrderMapper.selectAll(page,orderSearch));
        page.setRecords(saleOrderVoList);
        return page;
    }

    @Override
    public void add(SaleOrder saleOrder) {
        saleOrder.setCreateTime(Convert.toLocalDateTime(new Date()));
        System.out.println("输出一下时间："+saleOrder.getCreateTime());
        saleOrder.setUserId(SecurityUtils.getUserId().intValue());
       for(OrderDetail orderDetail : saleOrder.getOrderDetailList()) {
           orderDetail.setTotalAmount(orderDetail.getPrice().multiply(BigDecimal.valueOf(orderDetail.getTotal())));
       }
       saleOrderMapper.insert(saleOrder);
       int id=saleOrder.getOrderId();
       for(OrderDetail orderDetail:saleOrder.getOrderDetailList()) {
           orderDetail.setOrderId(id);
           orderDetailMapper.insert(orderDetail);
       }

    }

    @Override
    public Integer toExamine(int id,int newStatus) {
        if(sysUserMapper.selectPostNameByUserId(SecurityUtils.getUserId()).equals("普通员工")){
            return 0;
        }
        QueryWrapper<SaleOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",id);
        SaleOrder saleOrder = saleOrderMapper.selectOne(queryWrapper);
        saleOrder.setOrderStatus(newStatus);
        UpdateWrapper<SaleOrder> updateWrapper=new UpdateWrapper();
        updateWrapper.eq("order_id",id);
        return saleOrderMapper.update(saleOrder,updateWrapper);
    }

    @Override
    public List<SaleOrderItem> selectDetailedById(Integer orderId) {
        return orderDetailMapper.selectDetailedById(orderId);
    }

    public List<SaleOrderVo> modifyStatus(List<SaleOrderVo> list){
        for(SaleOrderVo saleOrderVo:list){
            switch (saleOrderVo.getOrderStatus()){
                case 0:
                    saleOrderVo.setStatus("待审核");
                    break;
                case 1:
                    saleOrderVo.setStatus("审核通过");
                    break;
                case 2:
                    saleOrderVo.setStatus("审核未通过");
                    break;
                case 3:
                    saleOrderVo.setStatus("退货");
                    break;
                case 4:
                    saleOrderVo.setStatus("部分退货");
                    break;
                case 5:
                    saleOrderVo.setStatus("订单完成");
                    break;

            }
        }
        return list;
    }
}
